﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Systems;
using AnguaRPGEngine.Library.Forms;

namespace AnguaRPGEngine.DND4eModule.Forms
{
    public partial class DNDGameEditor : Form
    {
        private int _QuestEditIndex;
        private DNDGame _Game;
        public DNDGame Game
        {
            get
            {
                return _Game;
            }
        }
        public DNDGameEditor(DNDGame game)
        {
            InitializeComponent();
            this._Game = game;
            if (this._Game == null)
            {
                _Game = new DNDGame();
                _Game.Quests = new List<Quest>();
                _Game.Players = new List<PlayerPCPair>();
                _Game.Areas = new List<AdventureArea>();
            }
        }

        private void DNDGameEditor_Load(object sender, EventArgs e)
        {

            #region Main Tab
            #region Main Area
            txtName.Text = _Game.Name;
            txtDescription.Text = _Game.Description;
            cboType.Text = Tools.ConvertEnumToString(_Game.GameType.ToString());
            cboType.Items.Clear();
            cboType.Items.Add("Other");
            cboType.Items.Add("Encounter");
            cboType.Items.Add("Dungeon Crawl");
            cboType.Items.Add("Module");
            cboType.Items.Add("Campaign");
            #endregion
            #region Players Area
            InitializePlayers();
            DisplayPlayers();
            #endregion 
            #endregion
            
            #region Intro Tab
            txtIntroduction.Text = _Game.Introduction;
            #endregion

            #region Quest Tab
            InitializeQuests();
            DisplayQuests();
            #endregion

            #region Area Tab
            InitializeAreas();
            DisplayAreas();
            #endregion

            #region Outtro Tab
            txtOuttro.Text = _Game.Outtro;
            #endregion
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            _Game.Name = txtName.Text;
        }

        private void txtDescription_TextChanged(object sender, EventArgs e)
        {
            _Game.Description = txtDescription.Text;
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            _Game.GameType = (GameTypes)cboType.SelectedIndex;
        }

        
        private bool IsGuid(string guid)
        {
            try
            {
                Guid G = new Guid(guid);
            }
            catch
            {
                return false;
            }

            return true;

        }

        #region Players / Player Characters
        private void InitializePlayers()
        {
            lvPlayers.ListView.Columns.Clear();
            PlayerPCPair.GetHeaders(lvPlayers.ListView);
            lvPlayers.OnButtonClicked += new Library.Controls.GenericListControl.ButtonClickedEventHandler(lvPlayers_OnButtonClicked);
        }
        private void DisplayPlayers()
        {
            lvPlayers.ListView.Items.Clear();
            foreach (PlayerPCPair playerPC in _Game.Players)
            {
                lvPlayers.ListView.Items.Add(playerPC.GetItem());
            }
        }
        void lvPlayers_OnButtonClicked(Library.Controls.GenericListControl.GenericListButtons Button, List<int> SelectedItemIndexes)
        {
            switch (Button)
            {
                case Library.Controls.GenericListControl.GenericListButtons.Add:
                    using (PlayerPCEditor ppcEditor = new PlayerPCEditor())
                    {
                        ppcEditor.PlayerPCs = GameSystem.Instance.GetPossiblePlayerPCPairs();
                        if (ppcEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            if (_Game.Players.IndexOf(ppcEditor.PlayerPC) == -1)
                            {
                                _Game.Players.Add(ppcEditor.PlayerPC);
                            }
                        }
                    }
                    break;
                case Library.Controls.GenericListControl.GenericListButtons.Edit:
                    using (PlayerPCEditor ppcEditor = new PlayerPCEditor())
                    {
                        ppcEditor.PlayerPCs = GameSystem.Instance.GetPossiblePlayerPCPairs();
                        ppcEditor.PlayerPC = _Game.Players[SelectedItemIndexes[0]];
                        if (ppcEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            int index = _Game.Players.FindIndex(p => p.PlayerInfo.Name == ppcEditor.PlayerPC.PlayerInfo.Name);
                            if (index > -1)
                            {
                                if (_Game.Players.IndexOf(ppcEditor.PlayerPC) == -1)
                                {
                                    _Game.Players[index] = ppcEditor.PlayerPC;
                                }
                            }
                        }
                    }
                    break;
                case Library.Controls.GenericListControl.GenericListButtons.Delete:
                    string Display = string.Format("Are you certain that you wish to delete th{0}?",
                        SelectedItemIndexes.Count > 1 ? string.Format("ese {0} player characters", SelectedItemIndexes.Count) : "is player character");
                    string Title = string.Format("Confirm{0}Deletion", SelectedItemIndexes.Count > 1 ? " Mulitple " : " ");
                    if (MessageBox.Show(this, Display, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        for (int i = SelectedItemIndexes.Count - 1; i > -1; i--)
                        {
                            _Game.Players.RemoveAt(SelectedItemIndexes[i]);
                        }
                    }
                    break;
            }
            DisplayPlayers();
        }
        #endregion
        #region Areas
        private void InitializeAreas()
        {
            lvAreas.Dock = DockStyle.Fill;
            lvAreas.ListView.Columns.Clear();
            AdventureArea.GetAreaHeaders(lvAreas.ListView);
            lvAreas.OnButtonClicked += new Library.Controls.GenericListControl.ButtonClickedEventHandler(lvAreas_OnButtonClicked);
        }
        void lvAreas_OnButtonClicked(Library.Controls.GenericListControl.GenericListButtons Button, List<int> SelectedItemIndexes)
        {
            switch (Button)
            {
                case Library.Controls.GenericListControl.GenericListButtons.Add:
                    using (AreaEditor aEditor = new AreaEditor())
                    {
                        if (aEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Game.Areas.Add(aEditor.Area);
                        }
                    }
                    break;
                case Library.Controls.GenericListControl.GenericListButtons.Edit:
                    using (AreaEditor aEditor = new AreaEditor())
                    {
                        aEditor.Area = _Game.Areas[SelectedItemIndexes[0]];
                        if (aEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Game.Areas[SelectedItemIndexes[0]] = aEditor.Area;
                        }
                    } break;
                case Library.Controls.GenericListControl.GenericListButtons.Delete:
                    string Display = string.Format("Are you certain that you wish to delete th{0}?",
                         SelectedItemIndexes.Count > 1 ? string.Format("ese {0} areas", SelectedItemIndexes.Count) : "is area");
                    string Title = string.Format("Confirm{0}Deletion", SelectedItemIndexes.Count > 1 ? " Mulitple " : " ");
                    if (MessageBox.Show(this, Display, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        for (int i = SelectedItemIndexes.Count - 1; i > -1; i--)
                        {
                            _Game.Areas.RemoveAt(SelectedItemIndexes[i]);
                        }
                    }
                    break;
            }
            DisplayAreas();
        }            
        private void DisplayAreas()
        {

            lvAreas.ListView.Items.Clear();
            foreach (AdventureArea Area in _Game.Areas)
            {
                Area.GetAreaItems(lvAreas.ListView);
            }
        }
        #endregion
        #region Quests
        private void InitializeQuests()
        {
            lvQuests.Dock = DockStyle.Fill;
            lvQuests.ListView.Columns.Clear();
            Quest.GetHeaders(lvQuests.ListView);
            lvQuests.OnButtonClicked += new Library.Controls.GenericListControl.ButtonClickedEventHandler(lvQuests_OnButtonClicked);
        }

        void lvQuests_OnButtonClicked(Library.Controls.GenericListControl.GenericListButtons Button, List<int> SelectedItemIndexes)
        {
            switch (Button)
            {
                case Library.Controls.GenericListControl.GenericListButtons.Add:
                    using (QuestEditor qEditor = new QuestEditor())
                    {
                        if (qEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Game.Quests.Add(qEditor.Quest);
                        }
                    }
                    break;
                case Library.Controls.GenericListControl.GenericListButtons.Edit:
                    using (QuestEditor qEditor = new QuestEditor())
                    {
                        qEditor.Quest = _Game.Quests[SelectedItemIndexes[0]];
                        if (qEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _Game.Quests[SelectedItemIndexes[0]] = qEditor.Quest;
                        }
                    }
                    break;
                case Library.Controls.GenericListControl.GenericListButtons.Delete:
                    string Display = string.Format("Are you certain that you wish to delete th{0}?",
                        SelectedItemIndexes.Count > 1 ? string.Format("ese {0} quests", SelectedItemIndexes.Count) : "is quest");
                    string Title = string.Format("Confirm{0}Deletion", SelectedItemIndexes.Count > 1 ? " Mulitple " : " ");
                    if (MessageBox.Show(this, Display, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        for (int i = SelectedItemIndexes.Count - 1; i > -1; i--)
                        {
                            _Game.Quests.RemoveAt(SelectedItemIndexes[i]);
                        }
                    }
                    break;
            }
            DisplayQuests();
        }
        private void DisplayQuests()
        {
            lvQuests.ListView.Items.Clear();
            foreach (Quest q in _Game.Quests)
            {
                lvQuests.ListView.Items.Add(q.GetItem());
            }
        }
        #endregion
    }
}
